import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

import Vant from 'vant';
import 'vant/lib/index.css';

Vue.use(Vant);

require('./assets/css/common.css'); //引入全局的base文件

//图片缩放
import preview from 'vue-photo-preview';
import 'vue-photo-preview/dist/skin.css';

let options = {
    fullscreenEl: false, //关闭全屏按钮
    maxSpreadZoom: 1.5, //手势时的最大缩放级别
    pinchToClose: true, //画廊的背景将逐渐淡出。手势完成后，图库将关闭
    closeOnVerticalDrag: true, //垂直拖动时以及图像未缩放时关闭图库。
    mouseUsed: false, //键盘左或右箭头键导航。
    closeEl: false, //关闭按钮
    tapToClose: true, //点击大图关闭
    zoomEl: false, //缩放按钮
};
Vue.use(preview, options);

//控制axios简单请求
import axios from 'axios';
import Qs from 'qs';
axios.defaults.headers['Content-Type'] =
    'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.transformRequest = [(obj) => Qs.stringify(obj)];
Vue.prototype.axios = axios;
Vue.prototype.qs = Qs;

Vue.filter('mobil', function (val) {
    if (!val) return '';
    return val.slice(0, 3) + '****' + val.slice(7);
});

//计算rem
Vue.prototype.rem = (input) => {
    return input * 46.875
}

Vue.prototype.errorImage = (e) => {
    e.target.src = require('./assets/images/default.png');
};

Vue.filter('timer', function (val) {
    const add0 = (m) => {
        return m < 10 ? '0' + m : m;
    };
    const time = new Date(Number(String(val) + '000'));
    const y = time.getFullYear();
    const m = time.getMonth() + 1;
    const d = time.getDate();
    return y + '-' + add0(m) + '-' + add0(d);
});

Vue.filter('money', function (val) {
    if (!val) return 0;
    val = val.toString().replace(/\$|,/g, '');
    if (isNaN(val)) {
        val = '0';
    }
    let sign = val == (val = Math.abs(val));
    val = Math.floor(val * 100 + 0.50000000001);
    let cents = val % 100;
    val = Math.floor(val / 100).toString();
    if (cents < 10) {
        cents = '0' + cents;
    }
    for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
        val =
            val.substring(0, val.length - (4 * i + 3)) +
            ',' +
            val.substring(val.length - (4 * i + 3));
    }
    return (sign ? '' : '-') + val + '.' + cents;
});

Vue.filter('moneyless', function (val) {
    if (!val) return 0;
    return val.split('.')[0];
});

//获取地址栏参数
Vue.prototype.getUrlKey = (name) => {
    // return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [""])[1].replace(/\+/g, '%20')) || null
    return (
        decodeURIComponent(
            (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(
                location.href
            ) || [''])[1].replace(/\+/g, '%20')
        ) || null
    );
};

// 获取用户信息
Vue.prototype.getUserInfo = () => {
    let userinfo = localStorage.userinfo;
    if (userinfo) {
        return JSON.parse(userinfo);
    } else {
        return false;
    }
};

//判断是否为微信浏览器
Vue.prototype.isWeiXin = () => {
    var ua = window.navigator.userAgent.toLowerCase();
    //通过正则表达式匹配ua中是否含有MicroMessenger字符串
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
};

//控制引入FastClick后input点击迟钝问题
import FastClick from 'fastclick';
FastClick.prototype.focus = function (targetElement) {
    var length;
    if (
        targetElement.setSelectionRange &&
        targetElement.type.indexOf('date') !== 0 &&
        targetElement.type !== 'time' &&
        targetElement.type !== 'month'
    ) {
        length = targetElement.value.length;
        targetElement.focus();
        targetElement.setSelectionRange(length, length);
    }
};
FastClick.attach(document.body);

//复制插件
import VueClipboard from 'vue-clipboard2';
Vue.use(VueClipboard);

// 数据加密
import CryptoJs from 'crypto-js';

// 加密
Vue.prototype.encrypt = (word) => {
    let key = CryptoJs.enc.Utf8.parse('1234567890junleb');
    let srcs = CryptoJs.enc.Utf8.parse(word);
    let encrypted = CryptoJs.AES.encrypt(srcs, key, {
        mode: CryptoJs.mode.ECB,
        padding: CryptoJs.pad.Pkcs7,
    });
    return encrypted.toString();
};

// 解密
Vue.prototype.decrypt = (word) => {
    let key = CryptoJs.enc.Utf8.parse('1234567890junleb');
    let decrypt = CryptoJs.AES.decrypt(word, key, {
        mode: CryptoJs.mode.ECB,
        padding: CryptoJs.pad.Pkcs7,
    });
    return CryptoJs.enc.Utf8.stringify(decrypt).toString();
};

// 正则富文本过滤
Vue.prototype.replaceText = (text) => {
    return text
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&quot;/g, '"')
        .replace(/quot;/g, "'")
        .replace(/&amp;/g, '&')
        .replace(
            /(\d+)px/g,
            (s) => (s.replace('px', '') / 46.875).toFixed(4) * 2 + 'rem'
        );
};

Vue.config.productionTip = false;

new Vue({
    router,
    store,
    render: (h) => h(App),
}).$mount('#app');
